<?php

namespace app\common\controller;


use app\common\oauth2\Server;

trait ApiApp
{
    use PublicApp;
    use Server;

    public function initialize()
    {
        $this->initOAuth2();
    }

    public function detail()
    {
        $id    = input('id');
        $where = input('s/a');
        $m     = new $this->model();
        if ($id) {
            $s = $m->getById($id);
        }
        else {
            $s = $m->_getWhere($where);
        }
        if ($s) {
            return js_return(1, 'success', $s);
        }
        return js_return(0, 'no data');
    }


    public function del_submit()
    {
        $m  = new $this->model();
        $id = input('id');
        $s  = $m->remove($id);
        return js_return($s[0], $s[1]);
    }


    public function contents_submit()
    {
        $m   = new $this->model();
        $id  = input('id');
        $opt = input('post.');
        if ($id) {
            $s = $m->modify($opt, $id);
        }
        else {
            $s = $m->add($opt);
        }
        return js_return($s[0], $s[1]);
    }


    public function get_list($callback = '')
    {
        $m     = new $this->model();
        $types = $m->gets();
        $count = $m->getsCount();
        $list  = [];
        foreach ($types as $key => &$val) {
            $callback && $callback($val);
            $list[] = $val;
        }
        return js_return(1, '', $list, $count);
    }

}
